<template>
  <div>
    <a-layout id="components-layout-demo-fixed-sider" style="min-height: 100vh">
      <f-sider></f-sider>
      <a-layout>
        <f-header></f-header>
        <a-layout-content
          :style="{ margin: '24px 16px 0', overflow: 'initial' }"
        >
          <div :style="{ padding: '24px', background: '#fff' }">
            <a-row type="flex">
              <div class="title">友链管理</div>
            </a-row>
            <a-row class="mt1r" type="flex" justify="space-between">
              <a-col class="disflex">
                <div>
                  <a-input-search
                    v-model="searchtitle"
                    placeholder="友链搜索"
                    style="width: 200px"
                    @search="onSearch"
                  />
                </div>
              </a-col>
              <a-col>
                <a-button type="primary" @click="add()">创建新友链</a-button>
              </a-col>
            </a-row>
            <a-table
              class="mt1r"
              :columns="columns"
              :rowKey="(data) => data.link_id"
              :dataSource="data"
            >
              <template slot="adsimg" slot-scope="data">
                <img :src="data.url" />
              </template>
              <template slot="operation" slot-scope="data">
                <a class="operation" @click="edit(data.link_id)">修改</a>
                <a class="operation" @click="del(data.link_id)">删除</a>
              </template>
            </a-table>
          </div>
        </a-layout-content>
        <f-footer></f-footer>
      </a-layout>
    </a-layout>
  </div>
</template>
<script>
import sider from "../../components/layout/Sider.vue";
import header from "../../components/layout/Header.vue";
import footer from "../../components/layout/Footer.vue";
const columns = [
  {
    title: "ID",
    dataIndex: "link_id",
  },
  {
    title: "标题",
    dataIndex: "link_title",
  },
  {
    title: "缩略图",
    scopedSlots: { customRender: "adsimg" },
  },
  {
    title: "链接",
    dataIndex: "link_address",
  },
  {
    title: "点击量",
    dataIndex: "hits",
  },
  {
    title: "创建时间",
    dataIndex: "link_create_time",
  },
  {
    title: "操作",
    scopedSlots: { customRender: "operation" },
  },
];

const data = [];
export default {
  metaInfo: {
    title: "友链管理",
  },
  data() {
    return {
      data,
      columns,
      visible: false,
      addvisible: false,
      title: "",
    };
  },
  components: {
    "f-header": header,
    "f-sider": sider,
    "f-footer": footer,
  },
  mounted() {
    this.$parent.tokenverify();
    this.getlst();
    this.setsiders();
  },
  methods: {
    //新增
    add() {
      this.$router.push("/Link/addlink");
    },
    //修改
    edit(link_id) {
      this.$router.push({
        path: "/Link/editlink",
        query: { link_id: link_id },
      });
    },
    del(e) {
      var _this = this;
      this.$confirm({
        title: "该操作不可逆，确定删除该记录?",
        okText: "是的",
        okType: "danger",
        cancelText: "取消",
        onOk() {
          _this
            .getAxios("/Admin/Ads/delads", {
              link_id: e,
              token: localStorage.getItem("token"),
            })
            .then((res) => {
              if (res.code == 1) {
                _this.getlst();
                this.$message.success("删除成功");
              } else if (res.code == 4001) {
                _this.$router.push("/Auth/login");
                _this.$message.warning("凭证失效，请重新登陆");
              } else {
                _this.$message.warning(res.msg);
              }
            });
        },
        onCancel() {},
      });
    },
    getlst() {
      const url = "/Admin/Link/getLink";
      const paramObj = {
        token: localStorage.getItem("token"),
      };
      this.$server.sl("GET", url, paramObj).then((res) => {
        if (res.code == 1) {
          this.data = res.data;
        } else if (res.code == 4001) {
          this.$router.push("/Auth/login");
          this.$message.warning("凭证失效，请重新登陆");
        } else {
          this.$message.warning("获取失败，请刷新重试");
        }
      });
    },
    searchAds: function () {},
    //权限选择
    handleRole(value) {
      this.role = value;
    },
    //菜单选中设置
    setsiders() {
      this.$store.commit("updateselectedKeys", "4");
    },
  },
};
</script>
<style scoped>
#components-layout-demo-fixed-sider .logo {
  height: 32px;
  background: rgba(255, 255, 255, 0.2);
  margin: 16px;
}
.title {
  margin: 0 0 10px;
  font-size: 20px;
}
.picurl {
  height: 3rem;
}
.operation:first-child {
  margin-right: 1rem;
}
img {
  height: 2rem;
  width: 2rem;
}
</style>